package com.apts.model;

import java.util.Calendar;
import java.util.Map;

import com.apts.jfinalbase.BaseModel;

public class TbRealRoots extends BaseModel<TbRealRoots> {
	private static final long serialVersionUID = -5310102820916603178L;

	public static final TbRealRoots dao = new TbRealRoots();

	public TbRealRoots findById(String realcode) {
		String sql = "select * from tb_real_roots where realcode=?";
		TbRealRoots record = TbRealRoots.dao.findFirst(sql, realcode);
		return record;
	}
	
	// 添加二维码
	public int ADD(Map<String, Object> map) {

		TbTwoCode tbtwocode = new TbTwoCode();

		String regcode = (String) map.get("regcode");
		String typecode = (String) map.get("typecode");
		String fromarea = (String) map.get("fromarea");
		String startdate = (String) map.get("startdate");
		String expdate = (String) map.get("expdate");
		String codePiNO = (String) map.get("codePiNO");
		String codescanftimes = (String) map.get("codescanftimes");
		String codeOvertime = (String) map.get("codeOvertime");

		String realcode = "" + (findmaxid() + 1);// 最大实体编号加一作为当前插入记录的实体编号

		tbtwocode.set("realcode", realcode).set("regcode", regcode).set("typecode", typecode).set("fromarea", fromarea)
				.set("fromarea", fromarea).set("startdate", startdate).set("expdate", expdate).set("codePiNO", codePiNO)
				.set("codeActivetime", Calendar.getInstance().getTime()).set("codescanftimes", codescanftimes)
				.set("codeOvertime", codeOvertime).save();
		System.out.println(findmaxid());
		return findmaxid();// 将刚插入的记录的id返回用于二维码网址生成
	}

	// 根据实体表的实体编号查找记录(貌似没什么用)
	public TbTwoCode findbyrealcode(String realcode) {

		String sql = "select * from tb_real_roots where realcode=? ";
		TbTwoCode tbtocode = TbTwoCode.dao.findFirst(sql, realcode);
		return tbtocode;

	}

	// 根据实体表每条记录的id查找记录
	public TbTwoCode findbyid(String id) {
		String sql = "select * from tb_real_roots where id=? ";
		TbTwoCode tbtocode = TbTwoCode.dao.findFirst(sql, id);
		return tbtocode;

	}

	// 根据实体表每条记录的id查找记录
	public TbTwoCode Scan(String id) {
		String sql = "select * from tb_real_roots where id=? ";
		TbTwoCode tbtocode = TbTwoCode.dao.findFirst(sql, id);
		int codescanftimes = tbtocode.getInt("codescanftimes");// 获取当前扫描次数
		tbtocode.set("codescanftimes", codescanftimes+1);// 每次扫描就增加一次扫描次数
		return tbtocode;

	}

	// 找出当前最大 的id
	public int findmaxid() {

		String sql = "select * from tb_real_roots where id=(select max(id) from tb_real_roots)";
		TbTwoCode tbtocode = TbTwoCode.dao.findFirst(sql);
		Integer maxid = tbtocode.getInt("id");

		System.out.println("最大id是：" + maxid);

		return maxid;

	}

	// 二维码激活
	public void Active(String id) {
		String sql = "select * from tb_real_roots where id=? ";
		TbTwoCode tbtocode = TbTwoCode.dao.findFirst(sql, id);
		tbtocode.set("codeActivetime", Calendar.getInstance().getTime()).update();// 将当前时间设置为激活时间

	}
}
